#include "CommonInfoModule.h"

QScopedPointer<CommonInfoModule> CommonInfoModule::ms_instance;
CommonInfoModule::CommonInfoModule(QObject *parent)
    : BaseModule(s_moduleName,parent)
    , m_timer(new QTimer(this))
{
    // connect singal solt
    connect(m_timer, &QTimer::timeout, this, &CommonInfoModule::dealInfo);
    connect(this , &CommonInfoModule::pollingIntervalChanged ,this, &CommonInfoModule::setTimePollingInterval);
}

CommonInfoModule::~CommonInfoModule()
{

}

CommonInfoModule *CommonInfoModule::instance()
{
    if (ms_instance.isNull()) {
        ms_instance.reset(new CommonInfoModule());
    }
    return ms_instance.data();
}

void CommonInfoModule::dealInfo()
{
    QString info;
    QScopedPointer<CommonInfoCollection> commonInfoCollection(new CommonInfoCollection(this));
    CommonInfo commonInfo;
    commonInfoCollection->getInfo(commonInfo);
    info = packageInfo(commonInfo);
    qDebug()<<info;
    emit sendInfo(info);
    qInfo()<<"CommonInfoModule::dealInfo: save common info";
}

void CommonInfoModule::setTimePollingInterval(qlonglong sec)
{
    m_timer->stop();
    m_timer->setInterval(secToMsec(sec));
    m_timer->start();
}


QString CommonInfoModule::packageInfo(const CommonInfo &commonInfo)
{
    QJsonObject commonInfoObject;
    commonInfoObject.insert(s_mainBoardSerialNumberKey,commonInfo.mainBoardSerialNumber);
    commonInfoObject.insert(s_networkMacAddressKey, commonInfo.networkMacAddress);
    commonInfoObject.insert(s_processorModelKey, commonInfo.processorModel);
    commonInfoObject.insert(s_graphicsCardModelKey, commonInfo.graphicsCardModel);
    commonInfoObject.insert(s_systemVersionKey, commonInfo.systemVersion);
    commonInfoObject.insert(s_systemSerialNumberKey, commonInfo.systemSerialNumber);
    commonInfoObject.insert(s_networkTypeKey, commonInfo.networkType);
    commonInfoObject.insert(s_hardDiskSerialNumberKey, commonInfo.hardDiskSerialNumber);
    commonInfoObject.insert(s_kernelVersionKey, commonInfo.kernelVersion);
    commonInfoObject.insert(s_systemUuidKey, commonInfo.systemUuid);
    commonInfoObject.insert(s_systemManufacturerKey,commonInfo.systemManufacturer);
    commonInfoObject.insert(s_systemProductNameKey,commonInfo.systemProductName);
    commonInfoObject.insert(s_machineIdKey, commonInfo.machineId);
    return QJsonDocument(commonInfoObject).toJson(QJsonDocument::JsonFormat::Compact);

}

void CommonInfoModule::printInfo(const CommonInfo &commonInfo)
{
    qDebug()<<"mainboardSerialNumber:"<<commonInfo.mainBoardSerialNumber;
    qDebug()<<"systemVersion:"<<commonInfo.systemVersion;
    qDebug()<<"systemSerialNumber:"<<commonInfo.systemSerialNumber;
    qDebug()<<"systemUuid:"<<commonInfo.systemUuid;
    qDebug()<<"kernelVersion:"<<commonInfo.kernelVersion;
    qDebug()<<"processorModel"<<commonInfo.processorModel;
    qDebug()<<"graphicsCardModel"<<commonInfo.graphicsCardModel;
    qDebug()<<"hardDiskSerialNumber"<<commonInfo.hardDiskSerialNumber;
    qDebug()<<"networkMacAddress"<<commonInfo.networkMacAddress;
    qDebug()<<"networkType"<<commonInfo.networkType;
    qDebug()<<"systemManufacturer:"<<commonInfo.systemManufacturer;
    qDebug()<<"systemProductName:"<<commonInfo.systemProductName;
}

void CommonInfoModule::updateProperty(const QJsonObject &conf,const QJsonObject &defaultConf)
{
    if(defaultConf.contains(moduleName())
       && defaultConf[moduleName()].isObject()
      )
    {
        QJsonObject moduleConf;
        QJsonObject moduleDefaultConf = defaultConf[moduleName()].toObject();
        if(conf.contains(moduleName())
            && conf[moduleName()].isObject())
        {
            moduleConf = conf[moduleName()].toObject();
        }
        qlonglong value;
        //	设置检测时长
        if(getValidConfInt(moduleConf,moduleDefaultConf,s_pollingIntervalKey,value) == FunctionSeccess)
        {
            setPollingInterval(value);
            qInfo()<<"module="<<moduleName()<<",property="<<s_pollingIntervalKey<<":"<<value;
        }
        else
        {
            qCritical()<<"module="<<moduleName()<<",property="<<s_pollingIntervalKey<<":read conf fail";
        }
    }
    else
    {
        qCritical()<<"module="<<moduleName()<<":update property fail";
    }
}

